package com.tanhua.management.controller;

import com.tanhua.management.pojo.UserFreeze;
import com.tanhua.management.service.UserFreezeService;
import com.tanhua.management.service.UsersService;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: PeiLin·Wang
 * @Description: 用户管理类
 * @Date: Created in 10:52 2021/1/17
 * @ModifiedBy:
 */
@RestController
@RequestMapping("manage/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private UserFreezeService userFreezeService;
    /**
     * 用户数据翻页
     *
     * @param page
     * @param pageSize
     * @param id
     * @param nickName
     * @param city
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "id", required = false) Long id,
                                                  @RequestParam(value = "nickname", required = false) String nickName,
                                                  @RequestParam(value = "city", required = false) String city,
                                                  @RequestHeader("Authorization") String token) {

        try {
            PageResult pageResult = this.usersService.queryByPage(page, pageSize, id, nickName, city, token);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户基本资料
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userID}")
    public ResponseEntity<UsersVo> queryUserInfoById(@PathVariable("userID") Long userId) {
        try {
            UsersVo usersVo = this.usersService.queryUserInfoById(userId);
            if (null != usersVo) {
                return ResponseEntity.ok(usersVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户冻结操作
     *
     * @param
     * @return
     */
    @PostMapping("freeze")
    public ResponseEntity<String> freeze(@RequestBody Map<String,Object> param) {
        try {
            Boolean bool = this.userFreezeService.freeze(param);
            if (bool){
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     * 用户解冻操作
     *
     * @param param
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity<String> unfreeze(@RequestBody Map<String,Object> param){
        try {
            Boolean bool = this.userFreezeService.unfreeze(param);
            if (bool){
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

}
